<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div>
    <a href="/book/toAdd">添加</a>
    <table border="1" cellspacing="0" cellpadding="0">
        <tr>
            <td>选择</td>
            <td>编号</td>
            <td>书名</td>
            <td>库存</td>
            <td>价格</td>
            <td>类目</td>
            <td>说明</td>
            <td>图片</td>
            <td>作者</td>
            <td>已售</td>
            <td>操作</td>
        </tr>

        <form th:action="@{/book/delBox}" method="post" onsubmit="return validateForm()">
            <tr th:each="book:${page.list}">
                <td><input type="checkbox" th:value="${book.id}" name="ids"></td>
                <td th:text="${book.id}"></td>
                <td th:text="${book.name}"></td>
                <td th:text="${book.pnum}"></td>
                <td th:text="${book.price}"></td>
                <td th:text="${book.category}"></td>
                <td th:text="${book.description}"></td>
                <td th:text="${book.imgurl}"></td>
                <td th:text="${book.author}"></td>
                <td th:text="${book.sales}"></td>
                <td>
                    <a th:href="@{/book(id=${book.id})}">修改</a>
                    <a href="javascript:void(0)" th:onclick="del([[${book.id}]])">删除</a>
                </td>
            </tr>
            <input type="submit" value="复选框删除">
        </form>


    </table>

    总的页数：<span th:text="${page.pages}"></span>&nbsp;总的记录数：<span th:text="${page.total}"></span>&nbsp;
    当前页的记录数：<span th:text="${page.pageSize}">&nbsp;</span> 当前页：<span th:text="${page.pageNum}"></span><br>
    <span th:if="${page.hasNextPage}">
        <a th:href="@{/book/list(pageNum=${page.nextPage})}">下一页</a>
    </span>&nbsp;&nbsp;
    <span th:each="p : ${#numbers.sequence(1,page.pages)}">
        <a th:href="@{/book/list(pageNum=${p})}" th:text="${p}"></a>
    </span>&nbsp;&nbsp;
    <span th:if="${page.hasPreviousPage}">
        <a th:href="@{/book/list(pageNum=${page.prePage})}">上一页</a>
    </span>
</div>

</body>
<script src="/jquery.js"></script>

<script>

    function validateForm(){
        if($("input[name='ids']:checked").length == 0){
            alert("必须选一个删除");
            return false;
        }
        if (!confirm("确定要删除吗？")) {
            alert("已取消删除！");
            return false;
        }
    }
    function del(id){
        if(confirm("确定删除吗？")){
            $.ajax({
                url:"/book/del/"+id,
                type:'delete',
                success:function (data){
                    if(data.flag){
                        alert("删除成功！");
                        location.href = "/book/list"
                    }else{
                        alert("删除失败！");
                    }
                }

            })
        }else{
            alert("已取消删除！");
        }
    }
</script>
</html>

